package com.briup.MR.InOutFormat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.InOutFormat.SeqWriteMR -D input=/user/hdfs/regex/regex.txt -D output=/user/hdfs/seqw_result
 */
public class SeqWriteMR extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Configuration conf=getConf();
        String input=conf.get("input");
        String output=conf.get("output");
        Job job= Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("SeqWriteMR");

        //指定块压缩
        SequenceFileOutputFormat.setOutputCompressionType(job, SequenceFile.CompressionType.BLOCK);
        //指定压缩类型
        SequenceFileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);
        //指定reduce或Map输出的键值生产SequenceFile格式的数据
        job.setOutputFormatClass(SequenceFileOutputFormat.class);
        TextInputFormat.addInputPath(job,new Path(input));
        SequenceFileOutputFormat.setOutputPath(job,new Path(output));
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(new ToolRunner().run(new SeqWriteMR(),args));
    }
}
